<?php 
require_once('../libraryfiles/config.php');
  //session_start();
if(isset($_POST['submit'])){
$allowedExts = array("jpg", "jpeg", "gif", "png");
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))) {
    if (file_exists("uploads/" . $_FILES["file"]["name"]))
      {
      $_SESSION['mg_same']= "<p>".$_FILES["file"]["name"] . " already exists. </p>";
	  header("location:index.php");
      exit();	
      }
    else{
		 if($_FILES['file']['name']!=""){
     	 move_uploaded_file($_FILES["file"]["tmp_name"],
      	"uploads/" . $_FILES["file"]["name"]);
        $filename = $_FILES['file']['name'];
		$select_update="SELECT *FROM tblimage";
		$run_select=mysql_query($select_update);
		$sel_assoc = mysql_fetch_assoc($run_select);
		//print_r($sel_assoc);die();
		if($sel_assoc['images']!=$filename){
		$insert_bg = mysql_query("INSERT INTO tblimage(images)VALUES('".$filename."')");
		$lastid = mysql_insert_id();
		mysql_query("UPDATE tbl_style SET id_img='".$lastid."'");			  
		}			
		$_SESSION['id']=$sel_assoc["id"];
		$image_name=$sel_assoc['images'];
		//$updat_image="UPDATE tbl_bg_image SET bg_nam='".$filename."' WHERE bg_status='".$status."'";
		mysql_query($updat_image);
		if($image_name){
			 unlink("uploads/".$image_name);
			}
			 header("location:index.php");
      }
	}
  }
else{
  $_SESSION['ms_invalid']= "<p>Invalid File and Field is required!</p>";
  header("location:index.php");
  }
}

?>